package com.ruoyi.elasticsearch.controller;


import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.elasticsearch.es.domain.DeviceDTO;
import com.ruoyi.elasticsearch.service.IDeviceService;
import com.ruoyi.elasticsearch.vo.DeviceVO;
import com.ruoyi.elasticsearch.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;


    /**
     * 设置设备状态
     * @param deviceVO
     * @return
     */
    @PutMapping("/status")
    public boolean setStatus(@RequestBody DeviceVO deviceVO){
        return  deviceService.setStatus( deviceVO.getSn(),deviceVO.getStatus() );
    }
    /**
     * 设置设备标签
     * @param deviceVO
     * @return
     */
    @PutMapping("/tags")
    public boolean setTags(@RequestBody DeviceVO deviceVO){
        return  deviceService.updateTags( deviceVO.getSn(),deviceVO.getTags());
    }

    /**
     * 分页查询设备
     * @param page
     * @param pageSize
     * @param sn
     * @param tag
     * @return
     */
    @GetMapping
    public Pager<DeviceDTO> findPage(@RequestParam(value="page",required = false,defaultValue = "1") Long page,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
                                     @RequestParam(value = "sn",required = false) String sn,
                                     @RequestParam(value = "tag",required = false)  String tag ){

        return deviceService.queryPage(page,pageSize,sn,tag,null);
    }


    /**
     * 接收设备断连信息
     * @param param
     */
    @PostMapping("/clientAction")
    public void clientAction(@RequestBody Map<String,String> param){
        System.out.println(param);
        String deviceId = param.get("clientid");  //提取设备id
        if( param.get("action").equals("client_connected") ){ //如果是联网
            System.out.println("联网");
            deviceService.updateOnLine(deviceId,true);
        }
        if( param.get("action").equals("client_disconnected") ){ //如果是断网
            deviceService.updateOnLine(deviceId,false);
            System.out.println("断网");
        }
    }

}
